package com.wxsh.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.baidu.aip.nlp.AipNlp;
import com.wxsh.Constants;

/**
 * 中文分词---不推荐
 * @author wxsh
 *
 */
public class BaiduTokenize {
	// 设置APPID/AK/SK
	public static final String APP_ID = "23758073";
	public static final String API_KEY = "7gN0hIuSMcsLZu6Km6qqjC9j";
	public static final String SECRET_KEY = "PvdgNCY7GZmpwKyr2zkHfQfGSalbq5SG";

	public static void main(String[] args) throws IOException, Exception {
		// 初始化一个AipNlp
		AipNlp client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);

		// 可选：设置网络连接参数
		client.setConnectionTimeoutInMillis(2000000);
		client.setSocketTimeoutInMillis(60000000);

		// 可选：设置代理服务器地址, http和socket二选一，或者均不设置
		// client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
		// client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理

		List<String> lines = FileUtils.readLines(new File("D:\\story.txt") , "UTF-8");
        List<String> destLines = new ArrayList<String>();
        for (String line : lines) {
        	// 调用接口
        	String result = getTokenizeStr(line, client);
        	destLines.add(result);
        	Thread.sleep(1000);
        }
        
        FileUtils.writeLines(new File("D:\\baidu_tokenize.txt"), "UTF-8", destLines, false);
	}
	
	private static String getTokenizeStr(String text, AipNlp client){
		try {
			JSONObject res = client.lexer(text, null);
			JSONArray jsonArr = res.getJSONArray("items");
			int size = jsonArr.length();
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < size; i++) {
				JSONObject obj = jsonArr.getJSONObject(i);
				String item = obj.getString("item");
				//String pos = obj.getString("pos");
				//sb.append(item+Constants.SPEECH+pos);
				sb.append(item);
				sb.append(Constants.SEPARATOR);
			}
			return sb.toString();
		} catch (Exception e) {
			System.out.println("Error:"+text);
		}
		return "";
	}
}
